package entryPoint;

/**
 *
 * @author Raman
 */
import gui.ChatInterface;
import gui.RegisterInterface;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.io.IOException;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.io.ObjectInputStream;
import java.io.PrintStream;
import java.net.Socket;

public class LoginInterface extends JFrame implements ActionListener {

  private JLabel userNameLabel, passWordLabel, mainHeading, status;
  private JTextField nameField;
  private JPasswordField passField;
  private JButton loginButton, registerButton, cancelButton;
  private JPanel mainPanel;
  
  //Layout stuff ------------------------------------------------------------
  private GridBagLayout gbl;
  private GridBagConstraints gbc;
  
  //Networking Stuff--------------------------------------------------------
  private Socket toServer;
  private PrintStream streamToServer;
  private ObjectInputStream streamFromServer;
  private String address = "127.0.0.1";
  private int port = 4000;

  //------------------------------------------------------------------------
  public LoginInterface() {
    super("Login Interface");
    initComponents();

    //Setting the Layout of mainPage
    mainHeading = new JLabel("Login Interface");
    mainHeading.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    mainHeading.setFont(new Font("Times New Roman", Font.BOLD, 30));
    add(mainHeading, BorderLayout.NORTH);

    add(mainPanel, BorderLayout.CENTER);

    status = new JLabel("Welcome Guest");
    status.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    status.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    status.setForeground(Color.RED);
    add(status, BorderLayout.SOUTH);
  }

  /** Used to initialize the GUI environment for the Login Interface*/
  private void initComponents() {
    mainPanel = new JPanel();
    gbl = new GridBagLayout();
    gbc = new GridBagConstraints();

    Insets inset = new Insets(5, 5, 5, 5);
    gbc.insets = inset;
    mainPanel.setLayout(gbl);

    //labels
    userNameLabel = new JLabel("USERNAME");
    passWordLabel = new JLabel("PASSWORD");

    //fields
    nameField = new JTextField(10);
    passField = new JPasswordField(10);

    //buttons
    loginButton = new JButton("LOGIN");
    loginButton.setMnemonic(KeyEvent.VK_L);
    registerButton = new JButton("REGISTER");
    registerButton.setMnemonic(KeyEvent.VK_R);
    cancelButton = new JButton("CANCEL");
    cancelButton.setMnemonic(KeyEvent.VK_C);

    gbc.gridx = 0;
    gbc.gridy = 0;
    mainPanel.add(userNameLabel, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    mainPanel.add(nameField, gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    mainPanel.add(passWordLabel, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    mainPanel.add(passField, gbc);

    gbc.gridx = 0;
    gbc.gridy = 2;
    mainPanel.add(loginButton, gbc);
    loginButton.addActionListener(this);
    loginButton.setActionCommand("login");

    gbc.gridx = 1;
    gbc.gridy = 2;
    mainPanel.add(registerButton, gbc);
    registerButton.setActionCommand("register");
    registerButton.addActionListener(this);

    gbc.gridx = 2;
    gbc.gridy = 2;
    mainPanel.add(cancelButton, gbc);
    cancelButton.setActionCommand("cancel");
    cancelButton.addActionListener(this);

    //frame related stuff
    setSize(new Dimension(400, 300));
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  /** Setter method to set the statusBar */
  public void setStatus(String status) {
    this.status.setText(status);
  }
    
//----------------------------------------------------------------------------


  @Override
  public void actionPerformed(ActionEvent event) {

    if (event.getActionCommand().equals("cancel")) {

      this.dispose(); //close the current frame

    } else if (event.getActionCommand().equals("register")) {

      RegisterInterface registerInterface = new RegisterInterface();//open registration interface 
      registerInterface.setResizable(false);
      this.dispose(); //close the current frame

    } else {  // if the action command is login
        try {
        toServer = new Socket(address, port);
        streamFromServer = new ObjectInputStream(toServer.getInputStream());
        streamToServer = new PrintStream(toServer.getOutputStream());

        //Send User data to Server
        streamToServer.println("LoginInfo");
        String userName = nameField.getText();
        char[] chpassWord = passField.getPassword();
        String passWord = new String(chpassWord);

        streamToServer.println(userName + ":" + passWord);

        //Read the message from the server
        String msgFromServer = (String) streamFromServer.readObject();
        System.out.println("Message From Server " + msgFromServer);
        
        if (msgFromServer.equals("Welcome")) {          
          ChatInterface chatInterface = new ChatInterface(userName);
          this.dispose(); // close the loginWindow
        } else {
          status.setText("Invalid Username or Password");
        }

      } catch (IOException ex) {

        status.setText(ex.getMessage());
        System.out.println("IOException in File " + ex.getCause());
        ex.printStackTrace();

      } catch (Exception ex) {

        System.out.println("LoginException " + ex);
        status.setText(ex.getMessage());
        ex.printStackTrace();
      }
    }
    
    

  }

//----------------------------------------------------------------------------
  public static void main(String args[]) {
    LoginInterface login = new LoginInterface();
    login.setResizable(false);
  }
}


